package kr.hkit.powerjava.chapter26.Young;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public abstract class AbsSerivce<T> {

	protected DaoInterface<T> dao;
	protected Scanner sc;
	protected Connection connection;

	public AbsSerivce(Scanner sc, Connection connection) {
		this.sc = sc;
		this.connection = connection;
	}

	public abstract T inputItemInfo(boolean gubun);
	public abstract void prnSubMenu();

	public void prnMenu() {
		int res = 0;
		T item = null;
		do {
			System.out.println("1.입력 2.삭제 3.수정 4.검색 5.리스트보기 6.종료");
			res = sc.nextInt();

			try {
				switch (res) {
					case 1: // 입력
						System.out.println("입력할 정보를 입력하시오.");
						item = inputItemInfo(true); // 받기!!
						dao.insertItem(item);
						break;
					case 2: // 삭제
						System.out.println("삭제할 정보를 입력하시오.");
						item = inputItemInfo(false);
						dao.deleteItem(item);
						break;
					case 3: // 수정
						System.out.println("수정할 정보를 입력하시오.");
						item = inputItemInfo(true);
						dao.updateItem(item);
						break;
					case 4: // 검색
						System.out.println("검색할 정보를 입력하시오.");
						item = inputItemInfo(false);
						item = dao.searchItem(item); //T타입이라서 item에 받아줘야함
						System.out.println(item); //출력도 해줘야함 
						break;
					case 5: // 리스트보기
						prnSubMenu();
						break;
					default:
						res = 7;
						break;
				}

			} catch (SQLException e) {
				System.err.printf("%s(%s)", e.getMessage(), e.getErrorCode());
			}
		} while (res <= 6);

	}

	public void prnItemList(Comparator<T> comparator, ArrayList<T> list) {
		Collections.sort(list, comparator);

		for (T d : list) {
			System.out.println(d);
		}
	}
}
